<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="close">
    <!----------------------------- 表单 ----------------------------->
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @focus="show = true"
        />
        <el-tree
          v-show="show"
          ref="tree"
          accordion
          class="treeBox"
          :data="treeData"
          :props="{ label: 'name' }"
          @node-click="selectNode"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <!------------------------------- footer插槽 ------------------------------>
    <template #footer>
      <el-button @click="$emit('close-box')">取消</el-button>
      <el-button type="primary" @click="addEmployee">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 导入员工聘用形式
import EnumType from '@/api/constant/employees'
// 导入获取部门数据的方法
import { getDepartments } from '@/api/departments'
// 导入转换为树形结构数据的方法
import { transformTreeData } from '@/utils/index'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      show: false, // 树形组织架构状态
      treeData: [], // 组织架构, 树形数据
      hireType: EnumType.hireType, // 聘用形式
      // --------------- 获取弹层数据所需参数 ----------------
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '用户姓名为1-10位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getDepartments()
  },
  methods: {
    // ------------------------ 关闭弹层执行 --------------------
    close () {
      // console.log('关闭弹层了')
      this.$emit('close-box')
      // 重置表单状态
      this.$refs.addForm.resetFields()
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      }
    },
    // ------------------------ 获取部门数据 ---------------------
    async getDepartments () {
      // 获取原始数据
      const { depts } = await getDepartments()
      // 树形数据转换
      this.treeData = transformTreeData(depts)
    },

    // 将点击的部门名字赋值给输入框
    selectNode (data) {
      // data => 点击的那个数据对象
      // console.log(data)
      this.formData.departmentName = data.name
      this.show = false
    },

    // -------------------------- 新增员工 ---------------------------
    async addEmployee () {
      try {
        /**
         * 1.整体校验
         * 2.调用接口添加
         * 3.关闭弹层, 重新渲染, 提示成功
         */
        await this.$refs.addForm.validate()
        // console.log('校验通过')
        await addEmployee(this.formData)
        this.$parent.getList()
        this.$emit('close-box')
        this.$message.success('新增员工成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.treeBox {
  width: 300px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
</style>
